package com.june.springframework.p2p;

import java.io.Serializable;

import javax.annotation.Resource;
import javax.jms.BytesMessage;
import javax.jms.Destination;
import javax.jms.MapMessage;
import javax.jms.StreamMessage;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

@Component
public class MessageProducer {
	
	@Resource
	private JmsTemplate jmsTemplate;
	
	@Resource
	private Destination textQueue;
	
	public void send(String message) {
		jmsTemplate.send(textQueue, session -> session.createTextMessage(message));
	}
	
	public void send(Serializable message) {
		jmsTemplate.send(textQueue, session -> session.createObjectMessage(message));
	}
	
	public void send(String name, Object value) {
		jmsTemplate.send(textQueue, session -> {
			MapMessage mapMessage = session.createMapMessage();
			mapMessage.setObject(name, value);
			return mapMessage;
		});
	}
	
	public void send(Object obj) {
		jmsTemplate.send(textQueue, session -> {
			BytesMessage bytesMessage = session.createBytesMessage();
			bytesMessage.writeObject(obj);
			return bytesMessage;
		});
	}
	
	public void send(byte[] data) {
		jmsTemplate.send(textQueue, session -> {
			StreamMessage streamMessage = session.createStreamMessage();
			streamMessage.writeBytes(data);
			return streamMessage;
		});
	}
}
